package pe.com.bbva.ge.vista.vaadin.ui.registrosolicitud;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;

import pe.com.bbva.ge.domain.configuracion.Lista;
import pe.com.bbva.ge.domain.solicitud.Destino;
import pe.com.bbva.ge.domain.solicitud.DetalleOtroServicio;
import pe.com.bbva.ge.vista.vaadin.ui.VentanaUI;
import pe.com.bbva.ge.vista.vaadin.ui.component.ComboBoxValor;
import pe.com.bbva.ge.vista.vaadin.ui.component.TxtNumerico;
import pe.com.bbva.ge.vista.vaadin.ui.utileria.AsignarDestino;
import pe.com.bbva.ge.vista.vaadin.ui.utileria.bean.PropiedadesTxtNumerico;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class RegistrarDetalleOtroServicioUI extends VentanaUI implements ClickListener, AsignarDestino {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9053754310538447212L;
	
	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private Table tblOtrosServicios;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private Button btnAgregarOtrosServicios;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	
	
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	
	private PropiedadesTxtNumerico propTxtNEnteros;
	
	private IndexedContainer container;
	
	private static final String COLUMNA_SERVICIO = "otrosServicios";
	private static final String COLUMNA_CANTIDAD = "cantidad";
	private static final String COLUMNA_ACCIONES = "acciones";
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public RegistrarDetalleOtroServicioUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		buildContainer();
	}
	
	private void buildContainer() {
		
		container = new IndexedContainer();
		container.addContainerProperty(COLUMNA_SERVICIO, ComboBoxValor.class,  null);
		container.addContainerProperty(COLUMNA_CANTIDAD, TxtNumerico.class,  null);
		container.addContainerProperty(COLUMNA_ACCIONES, Button.class,  null);
		tblOtrosServicios.setContainerDataSource(container);
		
		tblOtrosServicios.setColumnHeader(COLUMNA_SERVICIO, "Otros Servicos");
		tblOtrosServicios.setColumnHeader(COLUMNA_CANTIDAD, "Cantidad");
		tblOtrosServicios.setColumnHeader(COLUMNA_ACCIONES, "Acciones");
		
		tblOtrosServicios.setColumnWidth(COLUMNA_SERVICIO, 350);
		tblOtrosServicios.setColumnWidth(COLUMNA_CANTIDAD, 90);
		tblOtrosServicios.setColumnWidth(COLUMNA_ACCIONES, 80);
		
	}

	public void postConstruccion(){
		this.propTxtNEnteros = new PropiedadesTxtNumerico();
		this.propTxtNEnteros.setEsDecimal(false);

		mainLayout.addComponent(tblOtrosServicios);
		mainLayout.setExpandRatio(tblOtrosServicios, 1.0f);
		
		btnAgregarOtrosServicios.addListener((ClickListener)this);
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnAgregarOtrosServicios))
		{
			agregarOtroServicio();		
		}else if(event.getButton().getData() != null)
		{
			Object itemId = event.getButton().getData();
			quitarOtroServicio(itemId);
		}
	}
	
	private void agregarOtroServicio()
	{
		
		int itemIdNro = container.size();
		Item item = container.addItem(itemIdNro);
		
		ComboBoxValor cmbMateriales = new ComboBoxValor(Lista.CODIGO.OTROS_SERVICIOS);
		cmbMateriales.setWidth("335");
		
		TxtNumerico txtCantidadSolicitado = new TxtNumerico("", this.propTxtNEnteros);
		txtCantidadSolicitado.setWidth("75");
		txtCantidadSolicitado.setValue("0");
		
		item.getItemProperty(COLUMNA_SERVICIO).setValue(cmbMateriales);
		item.getItemProperty(COLUMNA_CANTIDAD).setValue(txtCantidadSolicitado);
		
		final  Button btnAcciones = new Button();
		btnAcciones.setData(itemIdNro);
		btnAcciones.setCaption("X");
		item.getItemProperty(COLUMNA_ACCIONES).setValue(btnAcciones);
		
		btnAcciones.addListener((ClickListener)this);
	}	
	
	private void quitarOtroServicio(Object itemId)
	{
		tblOtrosServicios.removeItem(itemId);
		container.removeItem(itemId);
	}

	@Override
	public void asignarDestino(Destino destino) {
		Iterator<?> iter = container.getItemIds().iterator();
		
		destino.setDetalleOtroServicios(new ArrayList<DetalleOtroServicio>());
		
		DetalleOtroServicio detalleOtroServicio;
		
		while (iter.hasNext()) {
			Object objectId = (Object) iter.next();
			Item item = container.getItem(objectId);
			TxtNumerico txt = (TxtNumerico) item.getItemProperty(COLUMNA_CANTIDAD).getValue();
			ComboBoxValor cmb = (ComboBoxValor) item.getItemProperty(COLUMNA_SERVICIO).getValue();
			
			detalleOtroServicio = new DetalleOtroServicio();
			detalleOtroServicio.setCantidad(new BigInteger((String) txt.getValue()));
			detalleOtroServicio.setServicio(cmb.obtenerValor());
			
			destino.addDetalleOtroServicio(detalleOtroServicio);			
		}
	}	

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// verticalLayout_1
		verticalLayout_1 = buildVerticalLayout_1();
		mainLayout.addComponent(verticalLayout_1);
		
		// tblOtrosServicios
		tblOtrosServicios = new Table();
		tblOtrosServicios.setImmediate(false);
		tblOtrosServicios.setWidth("780px");
		tblOtrosServicios.setHeight("300px");
		mainLayout.addComponent(tblOtrosServicios);
		mainLayout.setExpandRatio(tblOtrosServicios, 1.0f);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_1() {
		// common part: create layout
		verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("780px");
		verticalLayout_1.setHeight("40px");
		verticalLayout_1.setMargin(false);
		
		// btnAgregarOtrosServicios
		btnAgregarOtrosServicios = new Button();
		btnAgregarOtrosServicios.setCaption("+ Agregar Otros Servicios");
		btnAgregarOtrosServicios.setImmediate(true);
		btnAgregarOtrosServicios.setWidth("-1px");
		btnAgregarOtrosServicios.setHeight("-1px");
		verticalLayout_1.addComponent(btnAgregarOtrosServicios);
		verticalLayout_1.setComponentAlignment(btnAgregarOtrosServicios,
				new Alignment(34));
		
		return verticalLayout_1;
	}
}
